<script setup lang="ts">
import moment from "moment";
import { ElMessageBox, ElNotification } from "element-plus";
import { RefreshLeft, Search, Grid } from "@element-plus/icons-vue";
import { onMounted, reactive, ref } from "vue";
import { genData } from "@/views/marketForecast/loadForecasting/testData";
import { timeSlot } from "@/utils/index";
import VChart from "@/views/monitor/server/components/VChart.vue";
const loading = ref(false);
const queryForm = reactive({
  data: {
    activeTime: undefined,
    user: undefined,
    type: 1
  }
});
const types = reactive({
  data: [
    {
      value: 1,
      label: "系统历史负荷"
    },
    {
      value: 2,
      label: "系统气象预测"
    }
  ]
});
const shortcuts = [
  {
    text: "最近一周",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
      return [start, end];
    }
  },
  {
    text: "最近一月",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
      return [start, end];
    }
  },
  {
    text: "最近三月",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
      return [start, end];
    }
  }
];
const search = () => {
  let data = [];
  if (queryForm.data.type === 1) {
    defaultColums.data[0].label = "负荷日期";
  } else {
    defaultColums.data[0].label = "预测日期";
  }
  new Array(10).fill(1).forEach((item, index) => {
    data.push({
      dateTime: "2024-06-15",
      name: "负荷1",
      htime: moment().format("YYYY-MM-DD HH:mm:ss"),
      hmodel: "系统历史负荷",
      uType: index % 2 === 0 ? "手动修正" : "自动修正",
      hUser: "admin",
      hStatus: "成功"
    });
  });
  tableData.data = data;
};
const resetQuery = () => {
  queryForm.data = {
    activeTime: undefined,
    user: undefined,
    type: 1
  };
  tableData.data = [];
};
const pageObj = reactive({
  data: {
    currentPage: 1,
    pageSize: 10,
    total: 0
  }
});
const handleSizeChange = () => {};
const handleCurrentChange = async () => {};
const tableData = reactive({
  data: []
});
const times = ref(timeSlot(15));
const keys = ref(
  Array(96)
    .fill(1)
    .map((_, index) => index + 1)
    .map(i => `p${i > 9 ? i : "0" + i}`)
);
const dataFormat = data => {
  let days = ["2024-06-15"];
  let allData = [];
  let xData = [];
  days.forEach(i => {
    let item = {};
    item.dataDate = i + " 00:00:00";
    keys.value.forEach(key => {
      item[key] = null;
    });
    item.dataType = 1;
    allData.push(item);
    let item2 = {};
    item2.dataDate = i + " 00:00:00";
    keys.value.forEach(key => {
      item2[key] = null;
    });
    item2.dataType = 2;
    allData.push(item2);
  });
  for (let i = 0; i < allData.length; i++) {
    for (let j = 0; j < data.length; j++) {
      if (
        allData[i].dataDate === data[j].dataDate &&
        allData[i].dataType === data[j].dataType
      ) {
        allData[i] = Object.assign({}, allData[i], data[j]);
      }
    }
  }
  const ret = {};
  allData.forEach(i => {
    ret[i.dataDate] = {
      "1": {},
      "2": {}
    };
  });
  allData.forEach(i => {
    ret[i.dataDate][i.dataType + ""] = i;
  });

  let oneData = allData.filter(i => i.dataType === 1);
  let twoData = allData.filter(i => i.dataType === 2);
  let isd = [];
  let tsd = [];
  oneData.forEach(i => {
    times.value.forEach((z, index) => {
      xData.push(i.dataDate.split(" ")[0] + " " + z + ":00");
      isd.push({
        value: i[keys.value[index]]
      });
    });
  });
  twoData.forEach(i => {
    times.value.forEach((z, index) => {
      tsd.push({
        value: i[keys.value[index]]
      });
    });
  });
  let start = (1 - 96 / xData.length) * 100;
  options.value.xAxis.data = xData;
  options.value.dataZoom[0].start = start;
  options.value.dataZoom[1].start = start;
  options.value.dataZoom[0].end = 100;
  options.value.dataZoom[1].end = 100;

  options.value.series[0].data = isd;
  options.value.series[1].data = tsd;
};

const edit = row => {
  addDialog.value = true;
  dataFormat(genData("2024-06-15", "2024-06-15"));
};
const del = row => {
  ElMessageBox.confirm("确认将该数据还原?", "系统提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    ElNotification({
      title: "系统提示",
      message: "还原成功",
      type: "success"
    });
  });
};
const addDialog = ref(false);
const title = ref("数据对比");
const options = ref({
  dataZoom: [
    {
      show: true,
      type: "inside",
      start: 0,
      end: 100
    },
    {
      show: true,
      type: "slider",
      start: 0,
      end: 100
    }
  ],
  tooltip: {
    show: true,
    trigger: "axis"
  },
  legend: {
    show: true
  },
  xAxis: {
    type: "category",
    axisLabel: {
      //x轴文字的配置
      show: true
      // interval: 0,//使x轴文字显示全
    },
    splitLine: {
      show: true,
      interval: (index, value) => {
        if (value.indexOf("00:00:00") > -1) {
          return true;
        } else {
          return false;
        }
      },
      lineStyle: {
        type: "solid",
        width: 1.3
      }
    },
    boundaryGap: false,
    data: []
  },
  yAxis: {
    type: "value",
    min: "dataMin"
  },
  series: [
    {
      name: "操作前",
      type: "line",
      showSymbol: false,
      smooth: true,
      data: []
    },
    {
      name: "操作后",
      type: "line",
      showSymbol: false,
      smooth: true,
      data: []
    }
  ]
});
const init = () => {
  tableData.data = [];
};
const checkAll = ref(true);
const isIndeterminate = ref(false);
const checkedCities = ref([]);
const defaultColums = reactive({
  data: [
    {
      id: 1,
      label: "负荷日期",
      key: "dateTime",
      prop: "dateTime",
      width: "",
      align: "center"
    },
    {
      id: 2,
      label: "负荷名称",
      key: "name",
      prop: "name",
      width: "",
      align: "center"
    },
    {
      id: 3,
      label: "操作时间",
      key: "htime",
      prop: "htime",
      width: "",
      align: "center"
    },
    {
      id: 4,
      label: "操作模块",
      key: "hmodel",
      prop: "hmodel",
      width: "",
      align: "center"
    },
    {
      id: 5,
      label: "更新类型",
      key: "uType",
      prop: "uType",
      width: "",
      align: "center"
    },
    {
      id: 6,
      label: "操作人",
      key: "hUser",
      prop: "hUser",
      width: "",
      align: "center"
    },
    {
      id: 7,
      label: "操作状态",
      key: "hStatus",
      prop: "hStatus",
      width: "",
      align: "center"
    }
  ]
});
const colums = reactive({
  data: [
    {
      id: 1,
      label: "负荷日期",
      key: "dateTime",
      prop: "dateTime",
      width: "",
      align: "center"
    },
    {
      id: 2,
      label: "负荷名称",
      key: "name",
      prop: "name",
      width: "",
      align: "center"
    },
    {
      id: 3,
      label: "操作时间",
      key: "htime",
      prop: "htime",
      width: "",
      align: "center"
    },
    {
      id: 4,
      label: "操作模块",
      key: "hmodel",
      prop: "hmodel",
      width: "",
      align: "center"
    },
    {
      id: 5,
      label: "更新类型",
      key: "uType",
      prop: "uType",
      width: "",
      align: "center"
    },
    {
      id: 6,
      label: "操作人",
      key: "hUser",
      prop: "hUser",
      width: "",
      align: "center"
    },
    {
      id: 7,
      label: "操作状态",
      key: "hStatus",
      prop: "hStatus",
      width: "",
      align: "center"
    }
  ]
});
onMounted(() => {
  init();
  checkedCities.value = colums.data.map(item => item.prop);
});
const queryShow = ref(true);
const getList = () => {
  search();
};

const handleCheckAllChange = (val: boolean) => {
  checkedCities.value = val ? defaultColums.data.map(item => item.prop) : [];
  isIndeterminate.value = false;
  if (checkedCities.value.length) {
    colums.data = defaultColums.data;
  } else {
    colums.data = [];
  }
};
const handleCheckedCitiesChange = (value: string[]) => {
  const checkedCount = value.length;
  checkAll.value = checkedCount === defaultColums.data.length;
  isIndeterminate.value =
    checkedCount > 0 && checkedCount < defaultColums.data.length;
  let cols = [];
  defaultColums.data.forEach(i => {
    if (value.indexOf(i.prop) > -1) {
      cols.push(i);
    }
  });
  colums.data = cols;
};
</script>

<template>
  <div class="main h-[calc(100%-29px)]">
    <el-card v-loading="loading" class="w-full my-card-full">
      <el-row v-if="queryShow">
        <el-form :inline="true" :model="queryForm.data" label-width="0px">
          <el-form-item>
            <el-date-picker
              v-model="queryForm.data.activeTime"
              unlink-panels
              type="daterange"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              value-format="YYYY-MM-DD"
              date-format="YYYY-MM-DD"
              :shortcuts="shortcuts"
            />
          </el-form-item>
          <el-form-item>
            <el-select v-model="queryForm.data.type" style="width: 200px">
              <el-option
                v-for="item in types.data"
                :key="item.value"
                :value="item.value"
                :label="item.label"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="queryForm.data.user"
              placeholder="请输入操作人"
            />
          </el-form-item>
          <el-form-item class="ml-[10px]">
            <el-button type="primary" :icon="Search" @click="search"
              >查询</el-button
            >
            <el-button :icon="RefreshLeft" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <div class="w-full flex justify-between mb-[10px]">
        <div class="buttons flex align-center">
          <!--          <el-button type="primary" :icon="Plus" @click="add">新增</el-button>-->
          <!--          <el-button-->
          <!--            type="success"-->
          <!--            :icon="Edit"-->
          <!--            :disabled="!(multipleSelection.length === 1)"-->
          <!--            @click="editSelect"-->
          <!--          >修改</el-button-->
          <!--          >-->
          <!--          <el-button-->
          <!--            type="danger"-->
          <!--            :icon="Delete"-->
          <!--            :disabled="!multipleSelection.length"-->
          <!--            @click="delSelect"-->
          <!--          >删除</el-button-->
          <!--          >-->
          <!--            <el-button type="warning" :icon="Download" @click="exportData"-->
          <!--              >导出</el-button-->
          <!--            >-->
        </div>
        <div class="settings">
          <el-button-group class="ml-4">
            <el-button
              type="primary"
              :icon="Search"
              @click="queryShow = !queryShow"
            />
            <el-button type="primary" :icon="RefreshLeft" @click="getList" />
            <el-popover placement="bottom" :width="200" trigger="click">
              <template #reference>
                <el-button type="primary" :icon="Grid" />
              </template>
              <el-checkbox
                v-model="checkAll"
                :indeterminate="isIndeterminate"
                @change="handleCheckAllChange"
                >全选</el-checkbox
              >
              <div class="group-box w-full max-h-[240px] overflow-y-auto">
                <el-checkbox-group
                  v-model="checkedCities"
                  class="columns-show-group"
                  @change="handleCheckedCitiesChange"
                >
                  <el-checkbox
                    v-for="item in defaultColums.data"
                    :key="item.prop"
                    :label="item.prop"
                    >{{ item.label }}</el-checkbox
                  >
                </el-checkbox-group>
              </div>
            </el-popover>
          </el-button-group>
        </div>
      </div>
      <div class="w-full">
        <el-table :data="tableData.data" border width="100%">
          <el-table-column
            v-for="item in colums.data"
            :key="item.id"
            :label="item.label"
            :prop="item.key"
            :width="item.width"
            :align="item.align"
          />
          <el-table-column :fixed="'right'" label="操作" align="center">
            <template #default="scope">
              <el-button text type="primary" @click="del(scope.row)"
                >还原</el-button
              >
              <el-button text type="primary" @click="edit(scope.row)"
                >对比</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page w-full mt-[10px]">
        <el-pagination
          v-model:current-page="pageObj.data.currentPage"
          v-model:page-size="pageObj.data.pageSize"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageObj.data.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <el-dialog v-model="addDialog" :title="title" width="60%">
      <div class="w-full h-[500px]">
        <v-chart :options="options" />
      </div>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.my-card-full {
  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;

    .demo-tabs {
      display: flex;
      flex: 1;
      flex-direction: column;
      overflow: hidden;

      .el-tabs__content {
        flex: 1;

        .el-tab-pane {
          width: 100%;
          height: 100%;
        }
      }
    }
  }
}
</style>
